<?php
/**
 * Created by PhpStorm.
 * User: 69548
 * Date: 2018/7/25
 * Time: 15:41
 */

namespace app\api\model;

use think\Exception;
use think\Model;

class HotelSpuSpec extends Model
{
    public function getAdminHotelSpecList($where=[],$offset=0,$field="id,hotel_id,spec_name,spec_en_name,attr_json")
    {
        $where['is_deleted'] = 0;
        if(empty($offset)){
            return $this->field($field)->where($where)->select();
        }else{
            return $this->where($where)->paginate($offset);
        }

    }

    public function checkHotelSpecData(&$data)
    {
        if(empty($data['attr_json'])) return '';
        foreach ($data['attr_json'] as $k=>$v){
            if(empty($v['sub'])) continue;
            if(empty($v['id'])){
                //插入attr
                $attr_exist = db('hotel_spu_attr_value')->where(['attr_name'=>$v['name']])->find();
                if(empty($attr_exist)){
                    db('hotel_spu_attr')->insert(['attr_name'=>$v['name'],'create_time'=>time(),'update_time'=>time()]);
                    $attr_id = $this->getLastInsID();
                    $data['attr_json'][$k]['id'] = (string)$attr_id;
                }else{
                    $data['attr_json'][$k]['id'] = $attr_exist['id'];
                }
            }else{
                $attr_id = $v['id'];
            }

            foreach ($v['sub'] as $kk=>$vv){
                if(empty($vv['id'])){
                    //插入attr_value
                    $attr_value_exist = db('hotel_spu_attr_value')->where(['attr_id'=>$attr_id,'attr_value'=>$vv['name']])->find();
                    if(empty($attr_value_exist)){
                        db('hotel_spu_attr_value')->insert(['attr_id'=>$attr_id,'attr_value'=>$vv['name'],'create_time'=>time(),'update_time'=>time()]);
                        $attr_value_id = $this->getLastInsID();
                        $data['attr_json'][$k]['sub'][$kk]['id'] = (string)$attr_value_id;
                    }else{
                        $data['attr_json'][$k]['sub'][$kk]['id'] = $attr_value_exist['id'];
                    }
                }
            }
        }
    }

    public function createHotelSpec($data)
    {
        //是否重复
        if($this->where(['spec_name'=>$data['spec_name']])->find()){
            return "存在同名的规格！";
        }
        $res_msg = $this->checkHotelSpecData($data);
        if(!empty($res_msg)){return $res_msg;}
        $data['create_time'] = time();
        $data['update_time'] = time();
        $data['attr_json'] = !empty($data['attr_json'])?json_encode($data['attr_json']):'';
        $this->allowField(true)->insert($data);
    }

    public function updateHotelSpec($id,$data)
    {
        return $this->allowField(true)->save($data,['id'=>$id]);
    }

    public function delHotelSpec($id)
    {
        return $this->where('id',$id)->save([
            'is_deleted'=>1,
            'delete_time'=>time(),
        ]);
    }

    public function getHotelSpecDetail($id)
    {
        return $this->where('id',$id)->find();
    }

    //遍历获取所有的属性及其可选值
    public function getHotelSpuAttrs()
    {
        $where['is_deleted'] = 0;
        $attrs = db('hotel_spu_attr')->field('id,attr_name')->where($where)->select();
        $attr_values = db('hotel_spu_attr_value')->field('id,attr_id,attr_value,attr_en_value')->where($where)->order('weight desc')->select();
        foreach ($attrs as $key=>$value){
            $attrs[$key]['subValues'] = [];
            foreach ($attr_values as $k=>$v){
                if($value['id']==$v['attr_id']){
                    $attrs[$key]['subValues'][] = $v;
                    unset($attr_values[$k]);
                }
            }
        }

        return $attrs;
    }
}